﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Classes;
using Silvermoon.Visuals;
using System.Drawing;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control that contains collections of controls.
    /// See also <seealso cref="T:ScrollPanel"/>.
    /// </summary>
    public class Panel : Control, ICompositeControl
    {
        #region Fields

        private Color color = Color.Empty;
        private RectangleVisual background;

        #endregion
        #region ctor/dtor

        public Panel()
            : base()
        {
            Controls = new ControlCollection(this);
        }

        public override void Dispose()
        {
            foreach (var c in Controls) c.Dispose();
            if (background != null) background.Dispose();
            Visuals = null;
            Controls.Clear();
            base.Dispose();
        }

        #endregion
        #region Properties

        /// <summary>
        /// Gets  the list of child controls.
        /// </summary>
        public IList<Control> Controls { get; private set; }

        /// <summary>
        /// Gets or sets the background color.
        /// </summary>
        public Color Color
        {
            get { return color; }
            set
            {
                if (color != value) { color = value; Invalidate(InvalidationFlag.Color); }
            }
        }

        protected internal override IList<Control> SubControls
        {
            get { return Controls; }
        }

        #endregion
        #region Methods

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Color))
            {
                if (color != Color.Empty)
                {
                    if (background == null) background = new RectangleVisual();
                    background.Color = color;
                    Visuals = new Visual[] { background };
                }
                else
                {
                    Visuals = null;
                }
            }
            if (Layout != null) Layout(this, EventArgs.Empty);
            base.OnLayout();
        }

        #endregion
        #region events

        public event EventHandler Layout;
        #endregion

    }
}
